package com.smh.a05XmlDemo3;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

public class XpathDemo4 {
    public static void main(String[] args) throws DocumentException {
        //带属性查询
        SAXReader saxReader = new SAXReader();
        File file = new File("day32-code\\src\\com\\smh\\a04XmlDemo2\\users.xml");
        Document document = saxReader.read(file);
        //查的是属性
        List<Attribute> list = document.selectNodes("//@id");
        for (Attribute attribute : list) {
            System.out.println(attribute.getText());
        }
        System.out.println("-------------");
        //查的是带有指定属性的标签
        List<Element> list1 = document.selectNodes("//user[@id]");
        for (Element element : list1) {
            System.out.println(element.getName());
        }
        System.out.println("-----------");
        //查的是带有指定属性和值的标签
        List<Element> list2 = document.selectNodes("//user[@id='1']");
        for (Element element : list2) {
            System.out.println(element.getName());
        }


    }
}
